home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 8 / QRZ Ham Radio Callsign Database - Volume 8.iso / pc / files / dsp / srffttar.z / srffttar / SRFFT / fft2.c < prev    next >
C/C++ Source or Header  |  1991-08-26  |  6KB  |  305 lines

  1. /*--------------------------- fft2.c ---------------------------------- */
  2. /*                                                                      */
  3. /* Author:      Eyal Lebedinsky                                         */
  4. /* Date:        May 1990                                                */
  5. /* Version:     9 June 1991                                             */
  6. /*                                                                      */
  7. /* Example for using the output of fftg.c                               */
  8. /* To use this program you need to provide at least:                    */
  9. /*      move(x,y)                                                       */
  10. /*      draw(x,y,color)                                                 */
  11. /* Where x is 0-535 and y is 0-345 (or scale the constants...) and      */
  12. /* color is WHILE or BLACK. Then ignore the other vx*() calls.          */
  13. /*                                                                      */
  14. /* This program is released into the public domain.                     */
  15. /*                                                                      */
  16. /* Amiga modification and general cleanup by Udi Finkelstein, 26/8/91   */
  17. /*                                                                      */
  18. /*----------------------------------------------------------------------*/
  19.  
  20. #include <stdio.h>
  21.  
  22. #ifdef AMIGA
  23. #include <proto/exec.h>
  24. #include <proto/graphics.h>
  25. #include <proto/intuition.h>
  26. #include <intuition/intuition.h>
  27.  
  28. struct IntuitionBase *IntuitionBase;
  29. struct GfxBase *GfxBase;
  30. struct RastPort *rp;
  31. struct Screen *screen;
  32. struct Window *window;
  33.  
  34. struct NewScreen NS = {
  35.     0,0,
  36.     640,400,
  37.     2,
  38.     0,1,
  39.     HIRES|LACE,
  40.     CUSTOMSCREEN,
  41.     NULL,
  42.     "FFT demo",
  43.     NULL,
  44.     NULL
  45. };
  46.  
  47. struct NewWindow NW = {
  48.     0,1,
  49.     640,399,
  50.     0,1,
  51.     GADGETDOWN+GADGETUP,
  52.     SIMPLE_REFRESH+BORDERLESS+ACTIVATE,
  53.     NULL,
  54.     NULL,
  55.     "FFT demo",
  56.     NULL,
  57.     NULL,
  58.     5,5,
  59.     -1,-1,
  60.     CUSTOMSCREEN
  61. };
  62.  
  63. #define BLACK 0
  64. #define WHITE 1
  65.  
  66. #define move(x,y)    Move (rp, (long)(x), (long)((y) + 30))
  67.  
  68. void draw(x,y,c)
  69. int x, y, c;
  70. {
  71.     SetAPen(rp, c);
  72.     Draw (rp, (long)x, (long)(y + 30));
  73. }
  74.  
  75. /*
  76.  * Free all the resources allocated before we quit
  77.  */
  78. void cleanup()
  79. {
  80.     if (window) CloseWindow(window);
  81.     if (screen) CloseScreen(screen);
  82.     if (GfxBase) CloseLibrary(GfxBase);
  83.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  84. }
  85.  
  86. /*
  87.  * Exit with an error message
  88.  */
  89. void cleanexit(s, err)
  90. UBYTE *s;
  91. int err;
  92. {
  93.     if (*s) puts(s);
  94.     cleanup();
  95.     exit(err);
  96. }
  97.  
  98. #else
  99. /* presumably for MSDOS/Microsoft C */
  100.  
  101. #include <graph.h>
  102. #define move(x,y)    _moveto (x, y)
  103. #define draw(x,y,c)    _lineto (x, y)
  104. static struct videoconfig vc;
  105.  
  106. #endif
  107.  
  108. #define SC15 32768
  109.  
  110. #define M 8
  111. #define N (1 << M)
  112.  
  113. short x[N+1] = {0};                 /* fp(16,0)  */
  114. short qf[(N/2)+1] = {0};             /* fp(16,0)  */
  115.  
  116. static long in[N];                 /* fp(32,0)  */
  117.  
  118. main (argc)
  119. int argc;
  120. {
  121.     int i, j, k, m, n;
  122.     short dd, dh, dl, y, t ,o;              /* fp(16,0)  */
  123.     long qq, mf;              /* fp(32,0)  */
  124.     float flt;
  125.     char ns[30], fname[35];
  126.     FILE *fin;
  127.  
  128.     /* initialise   */
  129.  
  130.     m = M;
  131.     n = 1 << m;
  132.  
  133. #ifdef AMIGA
  134.     if (!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",33)))
  135.         cleanexit ("Open Intuition Library failed!\n", 10);
  136.  
  137.     if (!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",33)))
  138.         cleanexit ("Open Graphics Library failed!\n", 10);
  139.  
  140.     if (!(screen = (struct Screen *)OpenScreen(&NS)))
  141.         cleanexit ("Open Screen failed!\n", 10);
  142.  
  143.     NW.Screen = screen;
  144.  
  145.     if (!(window = (struct Window *)OpenWindow(&NW)))
  146.         cleanexit ("Open Window failed!\n", 10);
  147.  
  148.     rp = window->RPort;
  149.  
  150. #else
  151.  
  152.    if (_setvideomode (_HERCMONO) == 0) {
  153.       printf ("cannot set HERCMONO mode.\n");
  154.       exit (4);
  155.    }
  156.  
  157.    _getvideoconfig (&vc);
  158.  
  159. #endif
  160.  
  161.     /* end initialise */
  162.  
  163.     for (;;) {
  164.         printf ("\nWHICH DATA FILE TO BE USED ");
  165.         scanf (" %s", ns);
  166.         if (strcmp (ns, "end") == 0) {
  167. #ifdef AMIGA
  168.             cleanup();
  169. #else
  170. /*    vxclose ();*/
  171.       _setvideomode (_DEFAULTMODE);
  172. #endif
  173.             exit (0);
  174.         }
  175.         if (*ns == '\0')
  176.             strcpy (ns, "DATA1");
  177.  
  178.         strcpy (fname, ns);
  179.         strcat (fname, ".fft");
  180.         fin = fopen (fname, "r");
  181.         if (fin == NULL) {
  182.             perror (fname);
  183.             continue;
  184.         }
  185.  
  186.         fscanf (fin, " %u", &i);             /* ignore count */
  187.  
  188.         fscanf (fin, " %f", &flt);
  189.         mf = dh = dl = in[0] = dd = flt * 32;
  190.  
  191.         for (i = 1; i < n; ++i) {
  192.             fscanf (fin, " %f", &flt);
  193.             mf += in[i] = dd = flt * 32;
  194.             if (dd > dh) dh = dd;
  195.             if (dd < dl) dl = dd;
  196.         }
  197.         fclose(fin);
  198.  
  199.         y = mf / n;
  200.  
  201.         qq = dh - dl;
  202.         if (qq != 0) {   /* scale to maximum */
  203.             for (i = 0; qq < SC15; ++i, qq <<= 1);
  204.         }
  205.         else
  206.             i = 0;
  207.  
  208.         for (j = 0; j < n; ++j)
  209.             x[j+1] = (in[j] - y) << i;
  210.  
  211.         /* end preprocessing */
  212.  
  213. #ifdef AMIGA
  214.         move(0, 0);
  215.         ClearScreen(rp);
  216. #else
  217. /*
  218.    if (vxopen (NULL) < 0) {
  219.       fprintf (stderr, "could not open vx\n");
  220.       exit (0);
  221.    }
  222. */
  223. /* vxinit ();*/
  224. /* vxclear (BLACK);*/
  225.    _clearscreen (_GCLEARSCREEN);
  226. #endif
  227.  
  228.         move (15,     40*2);
  229.         draw (15+512, 40*2, WHITE);
  230.         draw (15+512, 1, WHITE);
  231.         draw (15,     1, WHITE);
  232.         draw (15,     40*2, WHITE);
  233.  
  234.         move (15, 40);
  235.         for (i = 0; i < n; ++i)
  236.             draw (16 + i + i, 40 - (x[i+1] >> 10), WHITE);
  237.  
  238.         move (15,       330);  /* X axis */
  239.         draw (15+13*40, 330, WHITE);
  240.         for (i = 0; i <= 130*4; i += 4) {
  241.             move (15+i, 330);
  242.             if (i%(50*4) == 0)           /* every 50 */
  243.                 draw (15+i, 330+15, WHITE);
  244.             else if (i%(10*4) == 0)           /* every 10 */
  245.                 draw (15+i, 330+10, WHITE);
  246.             else if (i%(5*4) == 0)           /* every 5  */
  247.                 draw (15+i, 330+5, WHITE);
  248.             else                   /* every 1  */
  249.                 draw (15+i, 330+2, WHITE);
  250.         }
  251.  
  252.         move (15, 330);  /* Y axis */
  253.         draw (15, 330-220, WHITE);
  254.         for (i = 0; i <= 220; i += 220/10) {
  255.             move (15, 330-i);
  256.             draw ((i%5) ? 15-5 : 15-10, 330-i, WHITE);
  257.         }
  258.  
  259. /* end screen layout */
  260.         if (argc > 1) {
  261.             short    xx[256];
  262.             long    lapse;
  263.  
  264.             memcpy (xx, x, sizeof (xx));
  265.             lapse = time (NULL);
  266.             for (i = 0; i < 10000; ++i) {
  267.                 memcpy (x, xx, sizeof (x));
  268.                 if (argc < 3)
  269.                     fft ();
  270.             }
  271.             lapse = time (NULL) - lapse;
  272.             printf ("time: %lu\n", lapse);
  273.             memcpy (x, xx, sizeof (x));
  274.         }
  275.  
  276.         fft ();
  277. /*
  278.         for (i = 0; i <= n/2; ++i) {
  279.             printf ("%u= %d ", i, qf[i]);
  280.             if (!(i % 5))
  281.             printf ("\n");
  282.         }
  283.         printf ("\n");
  284. */
  285.  
  286.         k = 0;
  287.         for (i = 1; i <= n/2; ++i) {
  288.             if (k < qf[i])
  289.             k = qf[i];
  290.         }
  291. /*        printf ("k= %d\n", k);*/
  292.         if (k == 0)
  293.         k = 1;
  294.  
  295.         t = 15;   o = 1 << (10 - m);
  296.         for (i = 1; i <= (n/2); ++i) {
  297.             j  = (qf[i] * 220L) / k;
  298.             t = t + o;
  299.             move (t, 330);
  300.             draw (t, 330 -  j, WHITE);
  301.         }
  302.  
  303.     }
  304. }
  305.